﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeoVision.Addin.Core.Spatial;
using GeoVision.Addin.Core.DataManagement;
using GeoVision.Addin.Helpers;

namespace GeoVision.Addin.UI.SQLSpatial
{
    public partial class BindShapeForm : Form
    {
        public BindShapeForm()
        {
            LocalizationHelper.SetCulture();
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void BindShapeForm_Load(object sender, EventArgs e)
        {

        }

        public VisShapeCreationInfo Execute(VisShapeCreationInfo info)
        {
            ctlPagenameField.DataSource = info.Schema.Columns;
            ctlTextField.DataSource = info.Schema.Columns;
            ctlFieldList.Items.Clear();
            foreach (var col in info.Schema.Columns)
            {
                ListViewItem item = new ListViewItem(col.Name);
                item.SubItems.Add(col.Type.ToString());
                item.Tag = col;
                ctlFieldList.Items.Add(item);
            }
            if (ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                info.RandomColors = ctlRandomBg.Checked;
                info.Lines = ctlLines.Checked;
                info.CreateNewPages = ctlCreateNewPages.Checked;
                if (info.CreateNewPages)
                {
                    if (ctlPagenameField.SelectedItem != null)
                        info.PagenameField = ctlPagenameField.SelectedValue.ToString();
                    info.LinkShapesToPages = ctlLinkShapestoPages.Checked;
                }
                else
                {
                    info.PagenameField = string.Empty;
                    info.LinkShapesToPages = false;
                }
                info.Selectedcolumns.Clear();
                foreach (ListViewItem li in ctlFieldList.Items)
                {
                    if (li.Checked)
                        info.Selectedcolumns.Add((Column)li.Tag);
                }
                if (ctlAddText.Checked)
                {
                    info.AddText = true;
                    if (ctlTextField.SelectedValue != null)
                        info.TextField = ctlTextField.SelectedValue.ToString();
                }
                else
                {
                    info.AddText = false;
                    info.TextField = "";
                }
                return info;
            }
            else return null;
        }

        private void ctlOK_Click(object sender, EventArgs e)
        {

        }

        private void ctlCreateNewPages_CheckedChanged(object sender, EventArgs e)
        {
            ctlLinkShapestoPages.Enabled = ctlCreateNewPages.Checked;
            ctlPagenameField.Enabled = ctlCreateNewPages.Checked;
        }

        private void ctlAddText_CheckedChanged(object sender, EventArgs e)
        {
            ctlTextField.Enabled = ctlAddText.Checked;
        }
    }
}
